'use client';

import { usePathname } from 'next/navigation';
import { useMemo } from 'react';

type BreadcrumbItem = {
  title: string;
  link: string;
};

// 这允许添加自定义标题
const routeMapping: Record<string, BreadcrumbItem[]> = {
  '/dashboard': [{ title: '首页', link: '/dashboard' }],
  '/dashboard/employee': [
    { title: '首页', link: '/dashboard' },
    { title: '员工', link: '/dashboard/employee' }
  ],
  '/dashboard/product': [
    { title: '首页', link: '/dashboard' },
    { title: '产品', link: '/dashboard/product' }
  ]
  // 根据需要添加更多自定义映射
};

export function useBreadcrumbs() {
  const pathname = usePathname();

  const breadcrumbs = useMemo(() => {
    // 检查是否有此确切路径的自定义映射
    if (routeMapping[pathname]) {
      return routeMapping[pathname];
    }

    // 如果没有精确匹配，则回退到从路径生成面包屑
    const segments = pathname.split('/').filter(Boolean);
    return segments.map((segment, index) => {
      const path = `/${segments.slice(0, index + 1).join('/')}`;
      return {
        title: segment.charAt(0).toUpperCase() + segment.slice(1),
        link: path
      };
    });
  }, [pathname]);

  return breadcrumbs;
}
